package com.alan.libapi.api.Impl;

import com.alan.libapi.api.ApiHttpRequest;
import com.alan.libapi.api.ApiResponse;
import com.alan.libapi.api.HttpEngine;
import com.alan.libapi.exception.NullParameterException;
import com.alan.libapi.model.persistent.ErrorInfo;
import com.alan.libapi.model.persistent.HttpType;
import com.alan.library.allog.KLog;
import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

/**
 * @className: ApiJsonParseHelper
 * @describtion: ApiJsonParseHelper
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/27 20:25
 */
public class ApiJsonParseHelper {
    /**
     * 解析JSON对象
     * @param jsonString 需要解析的Json字符串
     * @param resultClass 需要解析成的类
     * @return 返回对应类型的对象
     */
    public static  <T> ApiResponse<T> parseJson(String jsonString, Class<T> resultClass) throws JSONException {
        ApiResponse<T> apiResponse = new ApiResponse<>();
        JSONObject jsonObject = new JSONObject(jsonString);
        int retCode = jsonObject.getInt("retCode");
        JSONObject resultObject = jsonObject.getJSONObject("result");
        //Log.i("Tag", "resultObject = "+resultObject.toString());
        apiResponse.setRetCode(retCode);
        if (retCode == ApiResponse.SUCCESS_CODE) {
            apiResponse.setResult(JSON.parseObject(resultObject.toString(), resultClass));
        }else {
            apiResponse.setErrorResponse(JSON.parseObject(resultObject.toString(), ErrorInfo.class));
        }
        return apiResponse;
    }

    /**
     * 解析JSON列表
     * @param jsonString 需要解析的Json字符串
     * @param resultClass 需要解析成的类
     * @return 返回对应类型的List
     */
    public static <T> ApiResponse<List<T>> parseJsonList(String jsonString, Class<T> resultClass) throws JSONException{
        ApiResponse<List<T>> apiResponse = new ApiResponse<>();
        JSONObject jsonObject;
        jsonObject = new JSONObject(jsonString);
        int retCode = jsonObject.getInt("retCode");
        JSONObject resultObject = jsonObject.getJSONObject("result");
        //Log.i("Tag", "resultObject = "+resultObject.toString());
        apiResponse.setRetCode(retCode);

        if (retCode == ApiResponse.SUCCESS_CODE) {
            Object listJsonObject ;
            listJsonObject = resultObject.get("list");
            apiResponse.setResult(JSON.parseArray(listJsonObject.toString(), resultClass));
        }else {
            apiResponse.setErrorResponse(JSON.parseObject(resultObject.toString(), ErrorInfo.class));
        }
        return apiResponse;
    }

    /**
     * 发送请求并解析JSON对象
     * @param httpType http请求类型
     * @param requestObject  请求的数据对象
     * @param defaultErrorInfo 默认的错误信息
     * @return 返回对应类型的对象
     */
    public static <T> ApiResponse<T> requestAndParseJson(HttpType httpType, ApiHttpRequest<T> requestObject, ErrorInfo defaultErrorInfo) {
        ApiResponse<T> apiResponse = null;
        try {
            String responseString = null;
            try {
                responseString = HttpEngine.getInstance().execute(httpType, requestObject.getUrl(), requestObject.getParamList());
            } catch (IOException e) {
                KLog.e(e.toString());
            } catch (NullParameterException e) {
                KLog.e(e.toString());
            }
            //Log.i("Tag", "responseString = "+responseString);
            // 解析JSON数据
            if (responseString != null) {
                apiResponse = parseJson(responseString, requestObject.getResponseClass());
            }
        } catch (JSONException e) {
            apiResponse = null;
            KLog.e(e.toString());
        }
        if (apiResponse == null) {
            apiResponse = new ApiResponse<>();
            apiResponse.setRetCode(ApiResponse.DEFAULT_ERROR_CODE);
            apiResponse.setErrorResponse(defaultErrorInfo);
        }
        return apiResponse;
    }



    /**
     * 发送请求并解析JSONList对象
     * @param requestObject  请求的数据对象
     * @param defaultErrorInfo 默认的错误信息
     * @return
     */
    public static <T> ApiResponse<List<T>> requestAndParseJsonList(ApiHttpRequest<T> requestObject, ErrorInfo defaultErrorInfo) {
        ApiResponse<List<T>> apiResponse = null;
        try {
            String responseString = null;
            try {
                responseString = HttpEngine.getInstance().execute(HttpType.GET, requestObject.getUrl(), requestObject.getParamList());
            } catch (IOException e) {
                KLog.e(e.toString());
            } catch (NullParameterException e) {
                KLog.e(e.toString());
            }
            //Log.i("Tag", "responseString = "+responseString);
            // 解析JSON数据
            if (responseString != null) {
                apiResponse = parseJsonList(responseString, requestObject.getResponseClass());
            }
        } catch (JSONException e) {
            apiResponse = null;
            KLog.e(e.toString());
        }
        if (apiResponse == null) {
            apiResponse = new ApiResponse<>();
            apiResponse.setRetCode(ApiResponse.DEFAULT_ERROR_CODE);
            apiResponse.setErrorResponse(defaultErrorInfo);
        }
        return apiResponse;
    }

}
